/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelViewReport.java
 *
 * Created on Feb 18, 2012, 12:43:17 AM
 */
package GUI;

import Entity.ConnectionSQL;
import Entity.Report;
import Entity.User;
import Model.ReportTableModel;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author Duc
 */
public class PanelViewReport extends javax.swing.JPanel {
    private ResourceBundle messages;
    private User user;
    private ReportTableModel model;
    private InventoryManagerFrame parent;
    /** Creates new form PanelViewReport */
    public PanelViewReport(InventoryManagerFrame parent,ResourceBundle messages,User user) {
        initComponents();
        this.messages = messages;
        this.user = user;
        this.parent = parent;
        loadReport();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableReport = new javax.swing.JTable();
        btnView = new javax.swing.JButton();

        tableReport.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableReport.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableReportMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableReport);

        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(155, 155, 155)
                .addComponent(btnView)
                .addContainerGap(182, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnView)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        viewReport();
    }//GEN-LAST:event_btnViewActionPerformed

    private void tableReportMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableReportMouseClicked
        if(evt.getClickCount()==2){viewReport();}
    }//GEN-LAST:event_tableReportMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableReport;
    // End of variables declaration//GEN-END:variables
    private void loadReport(){
        ConnectionSQL cnn = new ConnectionSQL(messages);
        List<Report> listReport = cnn.getReportsByReceiver(user.getId());
        model = new ReportTableModel(listReport, messages);
        tableReport.setModel(model);
        model.fireTableDataChanged();
    }

    private void viewReport() {
        int select  = tableReport.getSelectedRow();
        if(select ==-1 ) JOptionPane.showMessageDialog(this, messages.getString("messagesNoSelect"));
        else{
            ConnectionSQL cnn = new ConnectionSQL(messages);
            Report report = model.get(select);
            cnn.changeStatusReport(report.getReportID());
            parent.setPanel( new PanelReportDetails(parent, messages, user, report));
        }
    }
}
